package com.ygou.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygou.pojo.Admininfo;
import com.ygou.pojo.Ordergoods;
import com.ygou.pojo.Orders;
import com.ygou.pojo.Refunds;
import com.ygou.service.OrderGoodsService;
import com.ygou.service.RefundsService;
import com.ygou.utils.AppendContentToFile;

@Controller
@RequestMapping(value="/refunds")
public class RefundsController {
	
	@Autowired
	private RefundsService refundsService;
	@Autowired
	private OrderGoodsService orderGoodsService;
	
	@RequestMapping(value="/list")
	public String refundlist(@RequestParam(value="currPageNo",defaultValue="1") Integer currPageNo,
			@RequestParam(value="pageSize",defaultValue="10") Integer pageSize,
			@RequestParam(value="createTime",required=false) String createTime,
			@RequestParam(value="isrefund",required=false) Integer isrefund,
			@RequestParam(value="userid",required=false) Integer userid,
			@RequestParam(value="type",defaultValue="1") Integer type,
			ModelMap model){
		PageHelper.startPage(currPageNo, pageSize);
		List<Refunds> refunds=refundsService.findrefund(createTime,isrefund,userid);
		PageInfo<Refunds> pages=new PageInfo<Refunds>(refunds,pageSize);
		model.put("pageInfo",pages);
		model.put("createTime",createTime==null?"":createTime);
		if(type==1){
			return "/admin/Refund";
		}else{
			return "/change";
		}
		
	}
	
	@RequestMapping(value="/refundinfo/{refundId}")
	public String findInfoById(@PathVariable Integer refundId,ModelMap model){
		Refunds refund=refundsService.selectByPrimaryKey(refundId);
		Ordergoods orderGoods=new Ordergoods();
		orderGoods.setOrderid(refund.getOrderid());
		List<Ordergoods> ogs=orderGoodsService.selectByExample(orderGoods);
		model.put("refund",refund);
		model.put("ogs",ogs);
		return "/admin/Refund_detailed";
	}
	
	/**
	 * 退款、驳回
	 * @return
	 */
	@RequestMapping(value="/refund",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean refund(HttpSession session,Refunds refund){
		/*******写入日志******/
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int id=0;
		if(admin!=null){
			id=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "对编号为"+refund.getOrderid()+"进行了退款/驳回");
		/*******END写入日志******/
		return refundsService.refund(refund);
	}
	
	/**
	 * 申请退款
	 * @param refund
	 * @return
	 */
	@RequestMapping(value="/addRefund",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean addRefund(Refunds refund){
		return refundsService.insertRefunds(refund);
	}
}
